package com.buaa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.buaa.common.Result;
import com.buaa.dto.AccidentSceneMapDTO;
import com.buaa.entity.AccidentSceneMap;
import com.buaa.entity.User;
import com.buaa.enums.ResponseCode;
import com.buaa.exception.ServiceException;
import com.buaa.mapper.AccidentSceneMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AccidentSceneMapServiceImpl extends ServiceImpl<AccidentSceneMapper, AccidentSceneMap> implements IService<AccidentSceneMap> {
    @Autowired
    private com.buaa.service.impl.UserServiceImpl userServiceImpl;

    public long countTotal(){
        return this.count();
    }
    public Map<String, Object> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        pageNum = (pageNum-1) * pageSize;
        // 创建一个分页对象，指定页码和每页大小
        IPage<AccidentSceneMap> page = new Page<>(pageNum, pageSize);
        QueryWrapper<AccidentSceneMap> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time")
                .last("LIMIT " + pageNum + "," + pageSize);
        Map<String,Object> res=new HashMap<>();
        try {
            //调用page方法，根据分页对象查询用户信息
            IPage<AccidentSceneMap> userPage = page(page, queryWrapper); // 调用 page 方法
            List<AccidentSceneMap> userList = userPage.getRecords();
            List<AccidentSceneMapDTO> dtoList = new ArrayList<AccidentSceneMapDTO>();
            BeanUtil.copyProperties(userList, dtoList, true);

            res.put("pagedata",userList);
            res.put("total",countTotal());
        } catch (ServiceException e) {
            new ServiceException(ResponseCode.DB_EXEC_ERROR.getKey(), ResponseCode.DB_EXEC_ERROR.getDesc());
        }
        return res;
        // 返回查询结果中的用户记录列表

    }

    public Result deleteBySceneId(Integer id, HttpServletRequest request){
        boolean result = false;
        try {
            result = removeById  (id);
        } catch (ServiceException e) {
            new ServiceException(ResponseCode.DB_EXEC_ERROR.getKey(), ResponseCode.DB_EXEC_ERROR.getDesc());
        }
        if (result)
            return Result.success(ResponseCode.SCENE_MAP_SUCCESS.getKey(), ResponseCode.SCENE_MAP_SUCCESS.getDesc(), "");
        else
            return Result.success(ResponseCode.SCENE_MAP_FAILURE.getKey(), ResponseCode.SCENE_MAP_FAILURE.getDesc(), "");

    }

    public Result updateMap(@RequestBody AccidentSceneMapDTO dto, HttpServletRequest request) {
        User currentOpUser = userServiceImpl.getOperateUser(request);
        AccidentSceneMap sceneMap = new AccidentSceneMap();
        dto.setMapTime(null);
        dto.setArriveTime(null);
        BeanUtil.copyProperties(dto, sceneMap, true);
        sceneMap.setRevisionTime(LocalDateTime.now());
        sceneMap.setReviser(currentOpUser.getUserName());
        boolean result = false;
        try {
            result = saveOrUpdate(sceneMap);
        } catch (ServiceException e) {
            new ServiceException(ResponseCode.DB_EXEC_ERROR.getKey(), ResponseCode.DB_EXEC_ERROR.getDesc());
        }

        if (result)
            return Result.success(ResponseCode.SCENE_MAP_SUCCESS.getKey(), ResponseCode.SCENE_MAP_SUCCESS.getDesc(), "");
        else
            return Result.success(ResponseCode.SCENE_MAP_FAILURE.getKey(), ResponseCode.SCENE_MAP_FAILURE.getDesc(), "");

    }

    public Result saveMap(@RequestBody AccidentSceneMapDTO dto, HttpServletRequest request) {
        User currentOpUser = userServiceImpl.getOperateUser(request);
        AccidentSceneMap sceneMap = new AccidentSceneMap();
        BeanUtil.copyProperties(dto, sceneMap, true);
        sceneMap.setCreateTime(LocalDateTime.now());
        sceneMap.setCreateBy(currentOpUser.getUserName());
        boolean result = false;
        try {
            result = saveOrUpdate(sceneMap);
        } catch (ServiceException e) {
            new ServiceException(ResponseCode.DB_EXEC_ERROR.getKey(), ResponseCode.DB_EXEC_ERROR.getDesc());
        }

        if (result)
            return Result.success(ResponseCode.SCENE_MAP_SUCCESS.getKey(), ResponseCode.SCENE_MAP_SUCCESS.getDesc(), "");
        else
            return Result.success(ResponseCode.SCENE_MAP_FAILURE.getKey(), ResponseCode.SCENE_MAP_FAILURE.getDesc(), "");
    }
}
